package com.cci.kangdao.controller;

import com.cci.kangdao.crmapi.CRMApiUtils;
import com.cci.kangdao.crmapi.CRMResponeEntity;
import com.cci.kangdao.service.RolesService;
import com.cci.kangdao.utilTool.ParameterTool;
import org.apache.commons.collections4.MapUtils;
import org.apache.log4j.Logger;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * @author William Du
 * @date 2019/4/30
 */
@RestController
public class RolesController {

    private Logger log = Logger.getLogger(this.getClass().getName());

    @Resource
    private RolesService rolesService;

    @RequestMapping(value = "/getOrganizationList.aspx")
    public String getOrganizationList(
            HttpServletRequest request,
            HttpServletResponse response){
        try{
            Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
            map.put("request", request);
            map.put("response", response);
            rolesService.getOrganizationList(map);
        }catch(Exception ex){
            log.error(ex.getMessage(),ex);
            ParameterTool.writeErrorResponse(response);
        }
        return null;
    }
    
    /**
     * 企业列表
     * Guidanz WWID用户完善用户信息
     */
    @RequestMapping(value = "/getCompanysGuidanz.aspx")
    public String getCompanysGuidanz(HttpServletRequest request, HttpServletResponse response){
        try{
            Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
            map.put("request", request);
            map.put("response", response);
            rolesService.getCompanysGuidanz(map);
        }catch(Exception ex){
            log.error(ex.getMessage(),ex);
            ParameterTool.writeErrorResponse(response);
        }
        return null;
    }
    
    
    

    @RequestMapping(value = "/checkOrganization.aspx")
    public String checkOrganization(
        HttpServletRequest request,
        HttpServletResponse response){
            try{
                Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
                map.put("request", request);
                map.put("response", response);
                rolesService.checkOrganization(map);
            }catch(Exception ex){
                log.error(ex.getMessage(),ex);
                ParameterTool.writeErrorResponse(response);
            }
            return null;
    }

    @RequestMapping(value = "/applyRole.aspx")
    public String applyRole(
            HttpServletRequest request,
            HttpServletResponse response){
        try{
            Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
            map.put("request", request);
            map.put("response", response);
            rolesService.applyRole(map);
        }catch(Exception ex){
            log.error(ex.getMessage(),ex);
            ParameterTool.writeErrorResponse(response);
        }
        return null;
    }

    @RequestMapping(value = "/cancelRole.aspx")
    public String cancelRole(
            HttpServletRequest request,
            HttpServletResponse response){
        try{
            Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
            map.put("request", request);
            map.put("response", response);
            rolesService.cancelRole(map);
        }catch(Exception ex){
            log.error(ex.getMessage(),ex);
            ParameterTool.writeErrorResponse(response);
        }
        return null;
    }

    @RequestMapping(value = "/getApplyOrderList.aspx")
    public void getApplyOrderList(
            HttpServletRequest request,
            HttpServletResponse response){
        try{
            Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
            map.put("request", request);
            map.put("response", response);
            rolesService.getApplyOrderList(map);
        }catch(Exception ex){
            log.error(ex.getMessage(),ex);
            ParameterTool.writeErrorResponse(response);
        }
    }


    @RequestMapping(value = "/getRoleOrderList.aspx")
    public void getRoleOrderList(
            HttpServletRequest request,
            HttpServletResponse response){
        try{
            Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
            map.put("request", request);
            map.put("response", response);
            rolesService.getRoleOrderList(map);
        }catch(Exception ex){
            log.error(ex.getMessage(),ex);
            ParameterTool.writeErrorResponse(response);
        }
    }

    @RequestMapping(value = "/getRoleNotifyList.aspx")
    public void getRoleNotifyList(HttpServletRequest request, HttpServletResponse response){
        try{
            Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
            map.put("request", request);
            map.put("response", response);
            rolesService.getRoleNotifyList(map);
        }catch(Exception ex){
            log.error(ex.getMessage(),ex);
            ParameterTool.writeErrorResponse(response);
        }
    }


    @RequestMapping(value = "/updateRoleNotifyReadStatus.aspx")
    public void updateRoleNotifyReadStatus(
            HttpServletRequest request,
            HttpServletResponse response){
        try{
            Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
            map.put("request", request);
            map.put("response", response);
            rolesService.updateRoleNotifyReadStatus(map);
        }catch(Exception ex){
            log.error(ex.getMessage(),ex);
            ParameterTool.writeErrorResponse(response);
        }
    }


    @RequestMapping(value = "/getRoleOrder.aspx")
    public void getRoleOrder(HttpServletRequest request, HttpServletResponse response){
        try{
            Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
            map.put("request", request);
            map.put("response", response);
            rolesService.getRoleOrder(map);
        }catch(Exception ex){
            log.error(ex.getMessage(),ex);
            ParameterTool.writeErrorResponse(response);
        }
    }

    @RequestMapping(value = "/approveRoles.aspx")
    public void approveRoles(HttpServletRequest request, HttpServletResponse response){
        try{
            Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
            map.put("request", request);
            map.put("response", response);
            rolesService.approveRoles(map);
        }catch(Exception ex){
            log.error(ex.getMessage(),ex);
            ParameterTool.writeErrorResponse(response);
        }
    }

    @RequestMapping(value = "/adminApproveRoles.aspx")
    public void adminApproveRoles(HttpServletRequest request, HttpServletResponse response){
        try{
            Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
            map.put("request", request);
            map.put("response", response);
            rolesService.adminApproveRoles(map);
        }catch(Exception ex){
            log.error(ex.getMessage(),ex);
            ParameterTool.writeErrorResponse(response);
        }
    }

    @RequestMapping(value = "/getRoleChannelList.aspx")
    public void getRoleChannelList(HttpServletRequest request, HttpServletResponse response){
        try{
            Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
            map.put("request", request);
            map.put("response", response);
            rolesService.getRoleChannelList(map);
        }catch(Exception ex){
            log.error(ex.getMessage(),ex);
            ParameterTool.writeErrorResponse(response);
        }
    }



    //给用户授权角色信息(在角色原有用户组下新增角色)   目前是移动技师使用
    @RequestMapping("/addRoleByUserId.aspx")
    public ModelAndView addRoleByUserId(HttpServletRequest request,
                                                        HttpServletResponse response) throws Exception {
        try{
            Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
            map.put("response", response);
            map.put("request", request);
            return rolesService.addRoleByUserId(map);
        }catch(Exception ex){
            log.error(ex.getMessage(),ex);

            String msg = "{\"status\": -99,\"msg\": \"未知错误\"}";
            ParameterTool.writeResponse(response, msg);
        }
        return null;
    }


    /**
     * @description: APP验证站长、技师、信息员在SF是否存在
     * @param: null
     * @return:
     * @exception:
     * @author: wunan
     * @company: Pactera Technology Co.,Ltd
     * @since: 2021/8/11 17:47
     * @version: V1.0
     */
    @RequestMapping(value = "/checkDCECUserRoleExist.aspx")
    public void checkDCECUserRoleExist(HttpServletRequest request, HttpServletResponse response) {
        try {
            Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
            Assert.notNull(MapUtils.getObject(map,"channel"));
            String channalName = MapUtils.getString(map,"channel");
            Assert.notNull(MapUtils.getObject(map,"phone"));
            String userPhone = MapUtils.getString(map,"phone");
            Assert.notNull  (MapUtils.getObject(map,"role"));
            Integer userRole = MapUtils.getInteger(map,"role");
            Assert.notNull  (MapUtils.getObject(map,"userName"));
            String userName = MapUtils.getString(map,"userName");
            Assert.notNull  (MapUtils.getObject(map,"serviceStationId"));
            Long serviceStationId = MapUtils.getLong(map,"serviceStationId");
            CRMResponeEntity crmResp = CRMApiUtils.getCRMServiceApi().validationDCECUserInfo(channalName, userPhone, userRole, userName,serviceStationId);
            ParameterTool.writeResponseForNull(response, crmResp.getStatus(), crmResp.getMessage(), crmResp.getData());
        } catch (Exception e) {
            e.printStackTrace();
            ParameterTool.writeErrorResponse(response);
        }
    }
}
